//
//  ViewForHeader.m
//  Project
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ModelForScrollView.h"  //M

#import "ViewForHeader.h"   //V
#import "CellForViewForHeader.h"

#import <UIImageView+WebCache.h>    //T

@interface ViewForHeader ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ViewForHeader

- (void)dealloc {
    
    self.collectionView.dataSource = nil;
    self.collectionView.delegate = nil;
    
    [_page release];
    [_collectionView release];
    [_flowLayout release];
    [_timer release];
 
    [_arrForModel release];
    [super dealloc];

}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

        self.arrForModel = [NSMutableArray array];
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:self.flowLayout];
        
        [self addSubview:self.collectionView];
        [_collectionView release];
        
        self.page = [[UIPageControl alloc] init];
        [self addSubview:self.page];
        [_page release];
        
        
        
        [self createCollectionView];
        
        [self createTimer];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.arrForModel.count) {
        
        [self.arrForModel addObject:[self.arrForModel firstObject]];
        [self.arrForModel insertObject:self.arrForModel[self.arrForModel.count - 2]  atIndex:0];
    }
    
    [self createPage];
}

- (void)createTimer {
    self.timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(timerChanged) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)invalTimer {
    [self.timer invalidate];
    self.timer = nil;
}

- (void)timerChanged {
  
    [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x + self.collectionView.frame.size.width, 0) animated:YES];
   
    if (self.collectionView.frame.size.width * (self.arrForModel.count - 2) <= self.collectionView.contentOffset.x) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
       
    }
    

    NSInteger temp = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
    NSInteger page = temp % (self.arrForModel.count - 2);
    self.page.currentPage = page;
    
}

// 点击轮播图时跳转界面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ModelForScrollView *model = [self.arrForModel objectAtIndex:indexPath.item];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"webViewURL" object:nil userInfo:@{@"url":model.html_url}];
}


// 当开始滑动时, 停止定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    
    [self invalTimer];
}

// 当结束滑动时, 开始定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    [self createTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (self.collectionView.frame.size.width * (self.arrForModel.count - 1) == scrollView.contentOffset.x) {
        self.collectionView.contentOffset = CGPointMake(self.collectionView.frame.size.width, 0);
    } else if (0 == scrollView.contentOffset.x) {
        self.collectionView.contentOffset = CGPointMake((self.arrForModel.count - 2) * self.collectionView.frame.size.width, 0);
    }
    self.page.currentPage = scrollView.contentOffset.x / self.collectionView.frame.size.width - 1;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrForModel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForViewForHeader *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    ModelForScrollView *model = [self.arrForModel objectAtIndex:indexPath.item];
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:[UIImage imageNamed:@"iconfont-zhanweitu"]];
    return cell;
    
}

- (void)createCollectionView {
    
    
    self.flowLayout.itemSize = CGSizeMake(self.frame.size.width, 180);
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.flowLayout.minimumLineSpacing = 0;
    
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView.pagingEnabled = YES;
    
    self.collectionView.contentOffset = CGPointMake(self.collectionView.frame.size.width, 0);
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[CellForViewForHeader class] forCellWithReuseIdentifier:@"pool"];
    
    
}

- (void)createPage {
    
    self.page.frame = CGRectMake(100, 140, self.frame.size.width - 200, 25);
    self.page.numberOfPages = self.arrForModel.count - 2;
    
}

@end
